package inet.sms.process;

import inet.db.pool.DBConfig;
import inet.db.pool.DBPool;
import inet.db.pool.SmppDBPool;
import inet.pdu.SMSRequest;
import inet.sms.network.NetworkConfig;
import inet.sms.network.SessionManager;
import inet.util.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Vector;
public class Server extends Thread {
	static BufferedReader keyboard;
	private static Logger logger = null;
	private static inet.util.Queue requestQueue = null;
	private static inet.util.Queue responseQueue = null;
	private static boolean result=true;
	private static boolean running = true;
	private String sFrom;
	private String sInfo;
	private String sTo;
	private SessionManager sessionManager;

	static {
		keyboard = new BufferedReader(new InputStreamReader(System.in));
	}

	public Server() {
		this.sessionManager = null;
		this.sFrom = "841685458982";
		this.sTo = "7045";
		this.sInfo = "ht abc";
		logger = new Logger(super.getClass().getName());
		requestQueue = new inet.util.Queue();
		responseQueue = new inet.util.Queue();
	}

	public void exit() {
		running = false;
		inet.util.Queue.storeAllQueueData();
		sms.util.Queue.storeAllQueueData();
		System.exit(1);
	}

	private String getParam(String prompt, String defaultValue) {
		String value = "";
		String promptFull = prompt;
		promptFull = promptFull
				+ ((defaultValue != null) ? " [" + defaultValue + "] " : "");
		System.out.print(promptFull);
		try {
			value = keyboard.readLine();
		} catch (IOException localIOException) {
		}

		if (value.compareTo("") == 0) {
			return defaultValue;
		}
		return value;
	}

	public static inet.util.Queue getRequestQueue() {
		return requestQueue;
	}

	public static inet.util.Queue getResponseQueue() {
		return responseQueue;
	}

	public static boolean isRunning() {
		return running;
	}

	public static void main(String[] args) {
		Server server = new Server();
		try {
			DBConfig.loadProperties();
			ServiceConfig.loadProperties();
			NetworkConfig.loadProperties();
			HelpConfig.loadProperties();
		} catch (IOException ex) {
			logger.error("main: " + ex.getMessage());
			return;
		}

		System.out.println("Server is starting ...");
		server.start();
	}



	public void run() {
	    if (!NetworkConfig.isNetworkEnabled())
	      new MOQueueScanner().start();
	    new RequestProcessor().start();
	    new ResponseProcessor().start();
	    logger.log("CodesList =" + ServiceConfig.codesList);
	    logger.log("Startup processor(s)");
	    for (int i = 0; i < ServiceConfig.processorsList.size(); i++) {
	      ((Processor)ServiceConfig.processorsList.get(i)).start();
	    }
	    logger.log("Init Network session");
	    if (NetworkConfig.isNetworkEnabled()) {
	      this.sessionManager = new SessionManager();
	      this.sessionManager.initSession();
	      this.sessionManager.startProcessors();
	    }
	    while (isRunning()) showMenu();
	  }


	private void showMenu() {
		System.out.println();
		System.out.println("D - Debug ON/OFF");
		System.out.println("T - Test process");
		System.out.println("C - Clear cache");
		System.out.println("P - Print cache");
		System.out.println("Q - Quit");
		System.out.println();
		String option = "";
		try {
			option = keyboard.readLine();
		} catch (Exception e) {
			logger.error("showMenu:" + e.getMessage());
		}

		if ("D".equals(option.toUpperCase())) {
			Logger.setVerbose(!(Logger.isVerbose()));
			System.out.println("Debug mode is "
					+ ((Logger.isVerbose()) ? "ON" : "OFF"));
		} else if ("T".equals(option.toUpperCase())) {
			test();
		} else if ("Q".equals(option.toUpperCase())) {
			exit();
		}
	}

	private void test() {
		SMSRequest request = new SMSRequest();
		request.setCommandCode("TEST");
		request.setId(new BigDecimal(0.0D));
		request.setRequestId(new BigDecimal(0.0D));
		request.setMobileOperator("NOT.SET");
		this.sFrom = getParam("Sender Number: ", this.sFrom);
		this.sTo = getParam("Service Number: ", this.sTo);
		this.sInfo = getParam("Info: ", "");
		if ((this.sFrom.length() <= 1) || (this.sTo.length() <= 1)
				|| (this.sInfo.length() <= 1))
			return;
		request.setSenderNumber(this.sFrom);
		request.setServiceNumber(this.sTo);
		request.setInfo(this.sInfo);
		requestQueue.enqueue(request);
	}
	
	
}
